<?php
session_start();
require_once 'db_connect.php';
require_once 'HerbManager.php';

// 检查用户是否已登录
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $herbManager = new HerbManager();
    $id = $_GET['id'] ?? null;
    
    if ($id) {
        $success = $herbManager->deleteHerb($id);
        
        if ($success) {
            echo json_encode(['success' => true, 'message' => '药材删除成功！']);
        } else {
            echo json_encode(['success' => false, 'message' => '删除失败，请重试']);
        }
        exit;
    }
}

echo json_encode(['success' => false, 'message' => '无效请求']);
?>